#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <QTextEdit>
#include <QLabel>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QPushButton>
#include <QUdpSocket>
#include <QTimer>
#include <QList>
#include <QVector>
#include <QHostAddress>
#include "base.h"

class mainWidget : public QWidget
{
    Q_OBJECT

public:
    mainWidget(QWidget *parent = 0);
    ~mainWidget();
    int msgProcess(MSG_t &msg, QHostAddress &recvAddres, int recvPort);       //消息处理
    void doLogin(MSG_t &msg, QHostAddress &recvAddres, int recvPort); //处理登录信息
    void doChat(MSG_t &msg, QHostAddress &recvAddres, int recvPort);  //处理正常消息
    void doQuit(MSG_t &msg, QHostAddress &recvAddres, int recvPort);  //处理退出信息
    void sendMsg(QString &sendmsg); //向当前所有客户端发送消息 sendmsg
    bool isInList(QHostAddress &recvAddres, int recvPort); //判断当前是否已有该客户端在聊天室列表中，存在返回 true, 不存在返回 false

public slots:
    void closeBtnClicked();
    void msgRecv(); //UDP 接收到数据处理

private:
    QLabel      *MsgLabel;  //消息框标签
    QTextEdit   *MsgEdit;   //消息框
    QPushButton *closeBtn;
    QVBoxLayout *mainLayout;

    MSG_t     recvMsg;
    struct ClientAdressPort{
        QHostAddress adress;
        int port;
    };

    QList<ClientAdressPort *> *ClientList;  //存放客户端信息容器
    QList<ClientAdressPort *>::iterator it; //遍历操作迭代器对象

    int port;
    QUdpSocket *udpSocket;
    QTimer      *timer;
};
#endif // MAINWIDGET_H
